<template>
	<view>
		<view v-if="requestList!=null" class="cu-list menu-avatar">
			<view class="cu-item" :class="modalName=='move-box-'+ index?'move-cur':''" v-for="(item,index) in requestList" :key="index"
			 @touchstart="ListTouchStart" @touchmove="ListTouchMove" @touchend="ListTouchEnd" :data-target="'move-box-' + index">
				<view class="cu-avatar round lg" :style="[{backgroundImage:'url('+item.sendFaceImage+')'}]"></view>
				<view class="content">
					<view class="text-grey">{{item.sendUsername}}</view>
					<view class="text-gray text-sm">请求添加你为好友!</view>
				</view>
				<view class="move">
					<view class="bg-blue" @tap="acceptUser(item.sendUserId)">接受</view>
					<view class="bg-red" @tap="rejectUser(item.sendUserId)">拒绝</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		created() {
			this.userId = this.$tool.getStorageSync("userInfo").id
			this.getRequestList()
		},
		data() {
			return {
				modalName: null,
				requestList: null
			}
		},
		methods: {
			// 拿到请求列表
			async getRequestList() {
				var res = await this.$request({
					baseURL: this.$tool.serverURL,
					url: 'user/queryFriendRequests',
					params: {
						acceptUserId: this.userId
					},
					type: 'get'
				})
				if (res.data != null) {
					this.requestList = res.data
				} else {
					uni.showToast({
						icon: 'none',
						title: 没有请求
					})
				}
			},

			// 接收请求
			async acceptUser(sendUserId) {
				var res = await this.$request({
					baseURL: this.$tool.serverURL,
					url: 'user/operFriendRequest',
					params: {
						acceptUserId: this.userId,
						sendUserId: sendUserId,
						operType: 1
					},
					type: 'get'
				})
				this.getRequestList()
			},

			// 忽略请求
			async rejectUser(sendUserId) {
				var res = await this.$request({
					baseURL: this.$tool.serverURL,
					url: 'user/operFriendRequest',
					params: {
						acceptUserId: this.userId,
						sendUserId: sendUserId,
						operType: 0
					},
					type: 'get'
				})
				this.getRequestList()
			},

			// ListTouch触摸开始
			ListTouchStart(e) {
				this.listTouchStart = e.touches[0].pageX
			},

			// ListTouch计算方向
			ListTouchMove(e) {
				this.listTouchDirection = e.touches[0].pageX - this.listTouchStart > 0 ? 'right' : 'left'
			},

			// ListTouch计算滚动
			ListTouchEnd(e) {
				if (this.listTouchDirection == 'left') {
					this.modalName = e.currentTarget.dataset.target
				} else {
					this.modalName = null
				}
				this.listTouchDirection = null
			}
		}
	}
</script>

<style>

</style>
